package com.fmsz.xmly.activitys.classify;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fmsz.xmly.R;
import com.fmsz.xmly.adapters.classify.tag.ClassifyTagPagerAdapter;
import com.fmsz.xmly.application.MyApplication;
import com.fmsz.xmly.entitys.classify.tag.ClassifyTagEntity;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ClassifyTagActivity extends AppCompatActivity implements Callback<ClassifyTagEntity> {

    private String categoryId;
    private String title;
    private TextView title_tv;
    private Toolbar toolbar;
    private ActionBar actionBar;
    private TabLayout tabLayout;
    private ViewPager pager;
    private TextView showAll_btn;
    private ClassifyTagPagerAdapter pagerAdapter;
    private List<ClassifyTagEntity.TagEntity.TagListEntity> titles;
    private LinearLayout load_tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classify_tag);
        Intent intent = getIntent();
        categoryId = intent.getStringExtra("categoryId");
        title = intent.getStringExtra("title");
        initView();
        initData();
    }

    private void initData() {
        MyApplication application = (MyApplication) getApplication();
        application.getClassifyService().getClassifyTagData(categoryId).enqueue(this);
    }

    private void initView() {
        title_tv = ((TextView) findViewById(R.id.title_classify_tag));
        title_tv.setText(title);
        toolbar = ((Toolbar) findViewById(R.id.toolbar_classify_tag));
        load_tip = ((LinearLayout) findViewById(R.id.load_tip));
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        tabLayout = ((TabLayout) findViewById(R.id.tab_layout_classify_tag));
        pager = ((ViewPager) findViewById(R.id.pager_classify_tag));
        showAll_btn = ((TextView) findViewById(R.id.showAll_btn));
        titles = new ArrayList<>();
        pagerAdapter = new ClassifyTagPagerAdapter(getSupportFragmentManager(),titles,categoryId);
        pager.setAdapter(pagerAdapter);
    }

    @Override
    public void onResponse(Call<ClassifyTagEntity> call, Response<ClassifyTagEntity> response) {
        if (response.body()!=null) {
//            Log.i("1620", "onResponse: "+(response.body().getCategoryContents().getList()==null));
//            titles.get(0).setTname("推荐");
            ClassifyTagEntity.TagEntity.TagListEntity entity = new ClassifyTagEntity().new TagEntity().new TagListEntity();
            entity.setTname("推荐");
            entity.setCategory_id(categoryId);
            titles.add(0,entity);
            titles.addAll(response.body().getTags().getList());
            pagerAdapter.notifyDataSetChanged();
            tabLayout.setupWithViewPager(pager);
        }
    }

    @Override
    public void onFailure(Call<ClassifyTagEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(this, "网络状态异常", Toast.LENGTH_SHORT).show();
    }
    public void hideProgress(){
        load_tip.setVisibility(View.GONE);
    }
}
